//
//  ViewController3.m
//  03.14-SaveData
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController3.h"
#import "StudentModel.h"
@interface ViewController3 ()<UITableViewDataSource,UITabBarDelegate>
@property(nonatomic,strong)NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController3

-(NSMutableArray *)stuList
{
    if (_stuList==nil) {
        //判断存档文件是否存在,如果之前已有存档文件，则从中解档出来以前的数据
        NSFileManager *fm=[NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self dataPath]]) {
            NSData *data=[NSData dataWithContentsOfFile:[self dataPath]];
            NSKeyedUnarchiver *unArch=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
            _stuList=[unArch decodeObjectForKey:@"Students"];
            
            [unArch finishDecoding];
        }
        else{
            _stuList=[NSMutableArray array];
        }
    }
    return _stuList;
   
}
-(NSString *)dataPath
{
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"students.arch"];
    return filePath;
}
//保存数据

-(void)saveData
{
    NSMutableData *mData=[NSMutableData data];
    NSKeyedArchiver *arch=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    [arch encodeObject:self.stuList forKey:@"Students"];
    [arch finishEncoding];
    [mData writeToFile:[self dataPath] atomically:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
}


- (IBAction)add:(id)sender {
    // 1. 将信息存储到对象中
    StudentModel *m = [[StudentModel alloc] initWithId:self.idTextField.text
                                               andName:self.nameTextField.text
                                                andAge:self.ageTextField.text];
    // 2. 将对象加入到数组中
    [self.stuList addObject:m];
    // 3. 保存
    [self saveData];
     [self.tableView reloadData];
}
- (IBAction)remove:(id)sender {
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 将该学生这个字典从数组中移除
            [self.stuList removeObject:m];
            break;
        }
    }
    // 4. 数组保存一下
    [self saveData];
     [self.tableView reloadData];
}
- (IBAction)modify:(id)sender {
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 修改该学生信息
            m.stuName = self.nameTextField.text;
            m.stuAge = self.ageTextField.text;
            break;
        }
    }
    // 4. 数组保存一下
    [self saveData];
     [self.tableView reloadData];
}
- (IBAction)find:(id)sender {
    // 1. 遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        // 2. 如果学号相同
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            // 3. 将查询到的学生信息显示到界面
            self.nameTextField.text = m.stuName;
            self.ageTextField.text = m.stuAge;
            break;
        }
    }
     [self.tableView reloadData];
}
- (IBAction)finAll:(id)sender {
    for (StudentModel *m in self.stuList)
    {
        NSLog(@"%@",m);
        [self.tableView reloadData];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    UILabel *label1=[cell.contentView viewWithTag:101];
    UILabel *label2=[cell.contentView viewWithTag:102];
    UILabel *label3=[cell.contentView viewWithTag:103];
    
    StudentModel *m=self.stuList[indexPath.row];
    
    label1.text=m.stuId;
    label2.text=m.stuName;
    label3.text=m.stuAge;
    return cell;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
